﻿

namespace IntellectShop.Common
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel.DataAnnotations;
	using System.Linq;
	using System.Text;
	using System.Web;

	public partial class Order
	{
		[DisplayNameLocalized("OrderID")]
		public int OrderID { get; set; }

		public string OrderOwnerID { get; set; }

		public List<OrderItem> OrderItems { get; set; }

		[DisplayNameLocalized("FirstName")]
		public string FirstName { get; set; }

		[DisplayNameLocalized("LastName")]
		public string LastName { get; set; }

		[DisplayNameLocalized("Address")]
		public string Address { get; set; }

		[DisplayNameLocalized("Notes")]
		public string Notes { get; set; }

		[DisplayNameLocalized("Country")]
		public string Country { get; set; }

		[DisplayNameLocalized("Email")]
		[Required(ErrorMessageResourceName = "EmailRequired", ErrorMessageResourceType = typeof(Resources.Errors))]
		public string Email { get; set; }

		[DisplayNameLocalized("Phone")]
		public string Phone { get; set; }

		[DisplayNameLocalized("CreatedBy")]
		public string CreatedBy { get; set; }

		[DisplayNameLocalized("Created")]
		public DateTime? Created { get; set; }

		[DisplayNameLocalized("Updated")]
		public DateTime? Updated { get; set; }

		[DisplayNameLocalized("UpdatedBy")]
		public string UpdatedBy { get; set; }

		public string DisplayName
		{
			get { return String.Format("{0} {1}", this.FirstName, this.LastName); }
		}

		public Common.Money Total
		{
			get
			{
				Common.Money result = new Common.Money { Amount = 0, Currency = Currency.RUB };
				this.OrderItems.ForEach(x => result += x.UnitPrice * x.Quantity);
				return result;
			}
		}

		public Order()
		{
			this.OrderItems = new List<OrderItem>();
		}

		public string Print()
		{
			StringBuilder buf = new StringBuilder();
			buf.AppendFormat("<p><strong>{0}</strong>: {1}</p>", Resources.Resources.OrderID, this.OrderID);
			buf.AppendFormat("<p><strong>{0}</strong>: {1}</p>", Resources.Resources.Email, this.Email);
			buf.AppendFormat("<p><strong>{0}</strong>: {1}</p>", Resources.Resources.FirstName, this.FirstName);
			buf.AppendFormat("<p><strong>{0}</strong>: {1}</p>", Resources.Resources.LastName, this.LastName);
			buf.AppendFormat("<p><strong>{0}</strong>: {1}</p>", Resources.Resources.Address, this.Address);
			buf.AppendFormat("<p><strong>{0}</strong>: {1}</p>", Resources.Resources.Country, this.Country);
			buf.AppendFormat("<p><strong>{0}</strong>: {1}</p>", Resources.Resources.Phone, this.Phone);
			buf.AppendFormat("<p><strong>{0}</strong>: {1}</p>", Resources.Resources.Notes, this.Notes);
			buf.Append("<br/>");
			if (this.OrderItems.Any())
			{
				buf.Append("<table>");
				buf.Append("	<tr>");
				buf.AppendFormat("		<th>{0}</th>", Resources.Resources.CommodityCode);
				buf.AppendFormat("		<th>{0}</th>", Resources.Resources.Name);
				buf.AppendFormat("		<th>{0}</th>", Resources.Resources.PriceEach);
				buf.AppendFormat("		<th>{0}</th>", Resources.Resources.Quantity);
				buf.Append("	</tr>");
				foreach (var item in this.OrderItems)
				{
					buf.Append("	<tr>");
					if (item.Commodity.CommodityType == CommodityType.Hardware)
					{
						var hardware = Common.Hardware.RetrieveByCommodityID(item.CommodityID);
						buf.AppendFormat("<td>{0}<br/>({1})</td>", item.Commodity.Code, hardware.InternalCode);
					}
					else
					{
						buf.AppendFormat("<td>{0}</td>", item.Commodity.Code);
					}

					buf.AppendFormat("<td>{0}</td>", item.Commodity.Name);
					buf.AppendFormat("<td style='text-align:right;'>{0}</td>", item.UnitPrice);
					buf.AppendFormat("<td style='text-align:right;'>{0}</td>", item.Quantity);
					buf.Append("	</tr>");
				}

				buf.Append("</table>");
				buf.Append("<br/>");
				buf.AppendFormat("<p><strong>{0}</strong>: {1}</p>", Resources.Resources.Total, this.Total);
			}

			return buf.ToString();
		}

		public static Order CreateLikeLast(HttpContextBase httpContext)
		{
			Common.Order lastOrder = Common.Order.RetrieveLast(httpContext.User.Identity.Name);
			Common.Order order = lastOrder != null ? new Common.Order
			{
				Email = lastOrder.Email,
				FirstName = lastOrder.FirstName,
				LastName = lastOrder.LastName,
				Address = lastOrder.Address,
				Country = lastOrder.Country,
				Phone = lastOrder.Phone
			} : new Common.Order();

			Common.Cart cart = Common.Cart.CreateCart(httpContext);
			order.OrderOwnerID = cart.CartOwnerID;
			cart.CartItems = cart.GetCartItems();
			foreach (var cartItem in cart.CartItems)
			{
				order.OrderItems.Add(new Common.OrderItem
				{
					CommodityID = cartItem.Commodity.CommodityID,
					Quantity = cartItem.Count,
					UnitPrice = cartItem.Commodity.Price,
				});
			}

			return order;
		}
	}
}
